package com.ruoyi.web.controller.customer;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.repairs.domain.CrmRepairsRecord;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.repairs.domain.CrmRepairs;
import com.ruoyi.repairs.service.ICrmRepairsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 维修Controller
 * 
 * @author hct
 * @date 2022-02-11
 */
@RestController
@RequestMapping("/repairs/repairs")
public class CrmRepairsController extends BaseController
{
    @Autowired
    private ICrmRepairsService crmRepairsService;

    /**
     * 查询维修列表
     */
    @PreAuthorize("@ss.hasPermi('repairs:repairs:list')")
    @GetMapping("/list")
    public TableDataInfo list(CrmRepairs crmRepairs)
    {
        startPage();
        List<CrmRepairs> list = crmRepairsService.selectCrmRepairsList(crmRepairs);
        return getDataTable(list);
    }

    /**
     * 导出维修列表
     */
    @PreAuthorize("@ss.hasPermi('repairs:repairs:export')")
    @Log(title = "维修", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CrmRepairs crmRepairs)
    {
        List<CrmRepairs> list = crmRepairsService.selectCrmRepairsList(crmRepairs);
        ExcelUtil<CrmRepairs> util = new ExcelUtil<CrmRepairs>(CrmRepairs.class);
        util.exportExcel(response, list, "维修数据");
    }

    /**
     * 获取维修详细信息
     */
    @PreAuthorize("@ss.hasPermi('repairs:repairs:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(crmRepairsService.selectCrmRepairsById(id));
    }

    /**
     * 新增维修
     */
    @PreAuthorize("@ss.hasPermi('repairs:repairs:add')")
    @Log(title = "维修", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CrmRepairs crmRepairs)
    {
        return toAjax(crmRepairsService.insertCrmRepairs(crmRepairs));
    }

    /**
     * 修改维修
     */
    @PreAuthorize("@ss.hasPermi('repairs:repairs:edit')")
    @Log(title = "维修", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrmRepairs crmRepairs)
    {
        return toAjax(crmRepairsService.updateCrmRepairs(crmRepairs));
    }

    /**
     * 修改维修人
     */
    @PreAuthorize("@ss.hasPermi('repairs:repairs:review')")
    @Log(title = "维修", businessType = BusinessType.UPDATE)
    @PostMapping("/review")
    public AjaxResult review(@RequestBody CrmRepairs crmRepairs)
    {
        CrmRepairs crmRepairsN=new CrmRepairs();
        crmRepairsN.setId(crmRepairs.getId());
        crmRepairsN.setRepairStatus(1);
        crmRepairsN.setUserId(crmRepairs.getUserId());
        return toAjax(crmRepairsService.updateCrmRepairs(crmRepairsN));
    }

    /**
     * 修改维修
     */
    @PreAuthorize("@ss.hasPermi('repairs:repairs:review')")
    @Log(title = "维修", businessType = BusinessType.UPDATE)
    @PostMapping("/finish")
    public AjaxResult finish(@RequestBody CrmRepairs crmRepairs)
    {
        CrmRepairs crmRepairsN=new CrmRepairs();
        crmRepairsN.setId(crmRepairs.getId());
        crmRepairsN.setRepairStatus(2);
        return toAjax(crmRepairsService.updateCrmRepairs(crmRepairsN));
    }


    /**
     * 删除维修
     */
    @PreAuthorize("@ss.hasPermi('repairs:repairs:remove')")
    @Log(title = "维修", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(crmRepairsService.deleteCrmRepairsByIds(ids));
    }
}
